/*
	Class Cookie.
	e Professional Co.,Ltd. (Thailand)., 2002
*/
function Cookie(neverExpire) {
	//	 Properties.
	this.neverExpire = Cookie.arguments.length > 0 ? (testBoolean = new Boolean(neverExpire) ? neverExpire : false ) : false ;

	//  Methods.
	this.hasCookie = hasCookie ;
	this.getNeverExpire = getNeverExpire ;
	this.setNeverExpire = setNeverExpire ;
	this.getCookieNames = getCookieNames ;
	this.destroyCookie = destroyCookie ;
	this.destroyAllCookie = destroyAllCookie ;
	this.setCookieValue = setCookieValue ;
	this.getCookieValue = getCookieValue ;
	this.getAllCookieValue = getAllCookieValue ;
}

function getNeverExpire() {
	return this.neverExpire ;
}

function setNeverExpire(neverExpire) {
	this.neverExpire = Cookie.arguments.length > 0 ? (testBoolean = new Boolean(neverExpire) ? neverExpire : false ) : false ;
}

/*
	Public Method hasCookie.
	Find cookie by cookieName. The method return boolean true when found or return false when other.
	If no argument, the method will return true when found any cookie.
*/
function hasCookie(cookieName) {
	if(hasCookie.arguments.length == 0) {
		return document.cookie.length > 0 ;
	} else {
		return (document.cookie.length > 0 && document.cookie.indexOf(cookieName+"=") >= 0)
	}
}

/*
	Public Method getCookieName.
	Return String Array of cookieName.
*/
function getCookieNames() {
	if(hasCookie())
		var cookieNames = document.cookie.toString().split(";") ;
	for(i = 0 ; i < cookieNames.length ; i++)
		cookieNames[i] = cookieNames[i].substring(0, (cookieNames[i].indexOf("=") >= 0 ? cookieNames[i].indexOf("=") : cookieNames.length)) ;

	return cookieNames ;
}

/*
	Public Method destroyCookie.
	Clear value containded in cookieName and expire it.
*/
function destroyCookie(name) {
	expDate =  new Date() ;
	expDate.setTime (expDate.getTime() - 1);  
	document.cookie = name + "=null; expires=" + expDate.toGMTString() ;
}

/*
	Public Method destroyAllCookie.
	Clear all cookie values.
*/
function destroyAllCookie() {
	if(hasCookie()) {
		var cookieNames = getCookieNames() ;
		for(i = 0 ; i < cookieNames.length ; i++) {
			destroyCookie(cookieNames[i]) ;
		}
	}
}

/*
	Public Method setCookieValue.
	Set value to cookie. Argument name passed as String, value passed as String. Method will replace exist value.
*/
function setCookieValue(name, value) {
	//	 Default expire at 30 days.
	expDate = new Date() ;
	expDate.setTime(expDate.getTime() + (30 * 24 * 60 * 60 * 1000)) ;
	document.cookie = name + " = " + escape(value) + " ;expires=" + expDate.toGMTString() ;
//	document.write('$'+document.cookie+'<br>') ;alert() ;
}

/*
	Public Method getCookieValue.
	Return the value contained in cookieName or null value when can not found cookieName.
*/
function getCookieValue(name) {
	if(hasCookie(name)) {
		var start = document.cookie.indexOf(name+"=") + name.length + 1 ;
		var end = document.cookie.indexOf(";", start) > 0 ? ( document.cookie.indexOf(";", start)) : document.cookie.length ;
		if(this.neverExpire)
			setCookieValue(name, unescape(document.cookie.substring(start, end))) ;
		return unescape(document.cookie.substring(start, end)) ;
	} else {
		return null ;
	}
}

/*
	Public Method getAllCookieValue.
	Return 2 Dimensions String Array, contain pair of name and value. No argument required.
*/
function getAllCookieValue() {
	var result ;
	if(hasCookie()) {
		result = new Array() ;
		var cookieNames = getCookieNames() ;
		for(i = 0 ; i < cookieNames.length ; i++) {
//			if(this.neverExpire)
//				setCookieValue(cookieNames[i], cookieNames[i], getCookieValue(cookieNames[i])) ;
			result[i] = new Array(cookieNames[i], getCookieValue(cookieNames[i])) ;
		}
	}
	return result ;
}